﻿@using Microsoft.AspNetCore.Components
@using Newbe.BookmarkManager.Services
@using WebExtensions.Net.Tabs
@using System.Text.Json
@using System.Text
@using Microsoft.Extensions.Options
@using Newbe.BookmarkManager.Services.Configuration
@using Newbe.BookmarkManager.Services.EventHubs
@using Newbe.BookmarkManager.Services.SimpleData
<Modal Title="@("Control Panel")"
       Visible="@Visible"
       OnOk="@HandleUserOptionsOk"
       OnCancel="@HandleUserOptionsCancel"
       AfterClose="OnAfterClose"
       MaskClosable="false"
       DestroyOnClose="true"
       Width="1200">
    <Collapse ExpandIcon="caret-right" Bordered="false">
        @if (_modal.AcceptPrivacyAgreement)
        {
            <ApplicationInsightPanel UserOptions="_modal"></ApplicationInsightPanel>
            <CloudBkPanel UserOptions="_modal"></CloudBkPanel>
            <PinyinPanel UserOptions="_modal"></PinyinPanel>
        }
        else
        {
            <Panel Header="User Privacy Agreement"
                   Active="true">
                <p>The following features may transfer some data to cloud servers, so please <a href="@StaticUrlOptions.Value.PrivacyAgreement" target="_blank">click here</a>, read and understand our User Privacy Agreement. After you confirm and agree to the content of the agreement click the button below to enable the advanced features.</p>
                <p>Feature shows as below:</p>
                <ul>
                    <li>Pinyin Support. You could search your bookmark title and your tags by PinYin</li>
                    <li>Tags Cloud Sync. You could sync your bookmark tags in multiple device</li>
                </ul>
                <Button OnClick="OnClickAgreeUserPrivacyAgreement" Type="primary">I agree to the User Privacy Agreement</Button>
            </Panel>
        }
        <HotTagsPanel UserOptions="_modal"></HotTagsPanel>
        <SystemActionsPanel></SystemActionsPanel>
    </Collapse>
</Modal>

@code {

    [Inject]
    public IUserOptionsService UserOptionsService { get; set; }

    [Inject]
    public IAfEventHub AfEventHub { get; set; }

    [Inject]
    public IOptions<StaticUrlOptions> StaticUrlOptions { get; set; }

    private ModalModel _modal = new();
    private bool _visible;
    private OneDriveStatics _oneDriveStatics = new();
    private GoogleDriveStatics _googleDriveStatics = new();

    [Parameter]
    public bool Visible
    {
        get => _visible;
        set
        {
            _visible = value;
            if (value)
            {
                InvokeAsync(LoadUserOptions);
            }
        }
    }

    [Parameter]
    public virtual EventCallback<bool> VisibleChanged { get; set; }

    [Parameter]
    public EventCallback OnClickResumeFactorySetting { get; set; }

    [Parameter]
    public EventCallback<OnUserOptionSaveArgs> OnUserOptionSave { get; set; }

    public record OnUserOptionSaveArgs(UserOptions Options);

    public class ModalModel
    {
        public bool AcceptPrivacyAgreement { get; set; }
        public PinyinFeature PinyinFeature { get; set; }
        public CloudBkFeature CloudBkFeature { get; set; }
        public HotTagsFeature HotTagsFeature { get; set; }
        public ApplicationInsightFeature ApplicationInsightFeature { get; set; }
    }

    private async Task OnClickAgreeUserPrivacyAgreement()
    {
        _modal.AcceptPrivacyAgreement = true;
        var userOptions = MapToUserOptions();
        await UserOptionsService.SaveAsync(userOptions);
    }

    private async Task CloseControlPanel()
    {
        await VisibleChanged.InvokeAsync(false);
    }

    private async Task HandleUserOptionsOk(MouseEventArgs e)
    {
        var userOptions = MapToUserOptions();

        await UserOptionsService.SaveAsync(userOptions);
        await AfEventHub.PublishAsync(new UserOptionSaveEvent
        {
            UserOptions = userOptions,
        });
        await OnUserOptionSave.InvokeAsync(new OnUserOptionSaveArgs(userOptions));
        await CloseControlPanel();
    }

    private UserOptions MapToUserOptions()
    {
        var userOptions = new UserOptions
        {
            AcceptPrivacyAgreementVersion = _modal.AcceptPrivacyAgreement
                ? Consts.PrivacyAgreementVersionDate
                : string.Empty,
            AcceptPrivacyAgreementBefore = _modal.AcceptPrivacyAgreement,
            PinyinFeature = _modal.PinyinFeature,
            CloudBkFeature = _modal.CloudBkFeature,
            HotTagsFeature = _modal.HotTagsFeature,
            ApplicationInsightFeature = _modal.ApplicationInsightFeature
        };
        return userOptions;
    }

    private async Task HandleUserOptionsCancel(MouseEventArgs e)
    {
        await CloseControlPanel();
    }

    private async Task OnAfterClose()
    {
        await CloseControlPanel();
    }

    private async Task LoadUserOptions()
    {
        var options = await UserOptionsService.GetOptionsAsync();
        _modal.PinyinFeature = options.PinyinFeature;
        _modal.CloudBkFeature = options.CloudBkFeature;
        _modal.HotTagsFeature = options.HotTagsFeature;
        _modal.ApplicationInsightFeature = options.ApplicationInsightFeature;
        _modal.AcceptPrivacyAgreement = options.AcceptPrivacyAgreement;
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await LoadUserOptions();
    }


}